package cc.mrbird.statistical.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.TimeUtil;
import cc.mrbird.statistical.service.EquipmentAnalysisService;
import cc.mrbird.statistical.service.UnitAnalysisService;
import cc.mrbird.system.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Api(description = "单位分析管理接口")
@Controller
public class UnitAnalysisController extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UnitAnalysisService unitAnalysisService;
    /* 1. 消防责任单位的各类消防设备的故障率统计和误报率统计(先不考虑)。该部分可以考虑在主窗口划分多个区域，
     可以同时显示每类消防设备的统计数据。*/
    @ApiOperation(value = "设备的故障率统计信息")
    @Log("设备的故障率统计信息")
    @RequestMapping(value = "unitAnalysis/findfailureRateByUnit" ,method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo findfailureRateByUnit(String nextTime) {//故障率统计  nextTime指定分析的时间区间
        Map<String,Float> failureRateStatisticsMap  =  new HashMap<>();
        User currentUser = super.getCurrentUser();
        try {
            if(StringUtils.isEmpty(nextTime)){
                nextTime = TimeUtil.ComputationTime("1年");
            }else {
                nextTime = TimeUtil.ComputationTime(nextTime);
            }
            failureRateStatisticsMap = this.unitAnalysisService.failureRateStatistics(nextTime,currentUser);
            return ResponseBo.ok(failureRateStatisticsMap);
        } catch (Exception e) {
            log.error("设备的故障率统计失败", e);
            return ResponseBo.error("查询设备的故障率统计失败，请联系网站管理员！");
        }
    }

    // 可以显示故障率和误报率(暂时不考虑)数据中最好TOP10和最坏TOP10设备(数量可以设置)
    @ApiOperation(value = "故障率最好TOP10和最坏TOP10设备")
    @Log("故障率最好TOP10和最坏TOP10设备")
    @RequestMapping(value = "unitAnalysis/findfailureRateRankings" ,method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo failureRateRankings(Integer number) { //设置显示数量
        try {
            if(number == null){
                number = 10;
            }

            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("故障率最好TOP10和最坏TOP10设备", e);
            return ResponseBo.error("查询故障率最好TOP10和最坏TOP10设备，请联系网站管理员！");
        }
    }

}
